import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginApi } from '@/api/user'
import type { LoginRes } from '@/api/user/user'

const useUserStore = defineStore(
  'user',
  () => {
    // as是ts中的类型断言
    // ！是ts中的非空断言
    const userinfo = ref({} as LoginRes)

    // 登录
    const login = async (data: any) => {
      const { data: res } = await loginApi(data)
      userinfo.value = res.data
    }

    // 退出登录
    const logout = () => {
      userinfo.value = {} as LoginRes
    }

    return { userinfo, login, logout }
  },
  {
    persist: true,
  },
)

export default useUserStore
